/**
 * 
 */
package hihocoder;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author zhongfang
 *
 */
public class 料理次元 {

	static class Food {
		Integer type;
		int pos;
		/**
		 * 需要相邻的类型
		 */
		ArrayList<Integer> needNext = new ArrayList<>();
		/**
		 * 需要相邻的数量
		 */
		int needCount;

		@Override
		public String toString() {
			String string = this.type + " " + this.needCount + " " + this.pos + " ";
			for (Integer integer : needNext) {
				string += integer + " ";
			}
			return string;
		}
	}

	static class Place {
		/**
		 * 已经在该位置放置的食物编号
		 */
		int placedFoodIndex = -1;

		/**
		 * 这个位置相邻的位置编号
		 */
		ArrayList<Place> nexts = new ArrayList<>();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int[][] plant = { { 0, 2 }, { 0, 3 }, { 1, 2 }, { 1, 5 }, { 2, 3 }, { 2, 5 }, { 2, 6 }, { 3, 4 }, { 3, 6 },
				{ 3, 7 }, { 4, 7 }, { 5, 6 }, { 5, 8 }, { 5, 9 }, { 6, 7 }, { 6, 9 }, { 6, 10 }, { 7, 10 }, { 7, 11 },
				{ 8, 9 }, { 9, 10 }, { 9, 12 }, { 10, 11 }, { 10, 12 } };
		Place[] places = new Place[13];
		for (int i = 0; i < 13; i++) {
			places[i] = new Place();
		}
		for (int[] is : plant) {
			places[is[0]].nexts.add(places[is[1]]);
			places[is[1]].nexts.add(places[is[0]]);
		}
		int T = scanner.nextInt();
		for (int i = 0; i < T; i++) {
			Food[] foods = new Food[5];
			for (int j = 0; j < 5; j++) {
				foods[j] = new Food();
				foods[j].type = scanner.nextInt();
				foods[j].needCount = scanner.nextInt();// 需要相邻的数量
				for (int j2 = 0; j2 < 6; j2++) {// 6个类型
					int t = scanner.nextInt();
					if (t == 1) {
						foods[j].needNext.add(j2);// 需要相邻的类型
					}
				}
				// System.out.println(foods[j]);
			}
			// 重置盘子,不放东西
			for (int j = 0; j < places.length; j++) {
				places[j].placedFoodIndex = -1;
			}
			dfs(0, places, foods);
			System.out.println(max);

		}

	}

	static int max = 0;

	private static void dfs(int i, Place[] places, Food[] foods) {
		if (i == 5) {// 全部安排完了，检查有多少个满足的
			int satisfyCount = 0;
			for (int j = 0; j < 5; j++) {// 五个食物
				int pos = foods[j].pos;
				// 检查周围个数
				int neiborCnt = 0;
				boolean[] neiborFoodType = new boolean[6];
				for (Place place : places[pos].nexts) {
					if (place.placedFoodIndex != -1) {// 这个邻居有放物品
						neiborCnt++;
						neiborFoodType[foods[place.placedFoodIndex].type] = true;// 有这一类食物
					}
				}
				if (neiborCnt < foods[j].needCount) {// 个数不满足
					continue;
				}
				boolean nextSatis = true;
				for (Integer b : foods[j].needNext) {
					if (!neiborFoodType[b]) {
						nextSatis = false;
						break;
					}
				}
				if (nextSatis) {
					satisfyCount++;
				}
			}
			if (max < satisfyCount) {
				max = satisfyCount;
				for (Food food : foods) {
					// System.out.println(food);
				}
			}
		} else {
			for (int j = 0; j < 13; j++) {
				if (places[j].placedFoodIndex == -1) {// 安排当前食物
					places[j].placedFoodIndex = i;
					foods[i].pos = j;

					dfs(i + 1, places, foods);

					places[j].placedFoodIndex = -1;
					foods[i].pos = -1;
				}
			}
		}
	}

}
